﻿using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.SaveDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 大社设置表仓储层
    /// </summary>
    public class OrgBigSettingRepository : SqlSugarRepository<OrgBigSettingEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgBigSettingRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<OrgBigSettingEntity>> GetPageList(OrgBigSettingQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<OrgBigSettingEntity>();
            //.Where(p => p.IsDeleted == 0)
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict.OrderBy(it => it.Id, OrderByType.Desc)
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<OrgBigSettingEntity>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }

        /// <summary>
        /// 修改订单归属门店
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateOrderSetting(OrgBigSettingEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgBigSettingEntity>()
                .SetColumns(it => new OrgBigSettingEntity()
                {
                    OrderDefaultOrgId = entity.OrderDefaultOrgId,
                    UpdatedTime = DateTime.Now,
                    UpdatedUserId = entity.UpdatedUserId,
                    UpdatedUserName = entity.UpdatedUserName
                }).Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }
        /// <summary>
        /// 修改产品设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateProdSetting(OrgSettingDto dto)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgBigSettingEntity>()
                .SetColumns(it => new OrgBigSettingEntity() { MarketProdMergeType = dto.OrgBigSettingEntity.MarketProdMergeType, CruiseProdMergeType = dto.OrgBigSettingEntity.CruiseProdMergeType, IsEditProd = dto.OrgBigSettingEntity.IsEditProd, IsGiveInsurance = dto.OrgBigSettingEntity.IsGiveInsurance, UpdatedTime = DateTime.Now, UpdatedUserId = dto.OrgBigSettingEntity.UpdatedUserId, UpdatedUserName = dto.OrgBigSettingEntity.UpdatedUserName })
                .Where(it => it.Id == dto.OrgBigSettingEntity.Id).ExecuteCommandAsync() > 0;
            //追加产品类型赠品设置
            foreach (var GiftSetting in dto.OrgGiftSettingList)
            {

               var  res=await _unitOfWork.ModifyClient.Updateable<OrgGiftSettingEntity>()
                    .SetColumns(it => new OrgGiftSettingEntity() { IsClose = GiftSetting.IsClose, ProductTypeId = GiftSetting.ProductTypeId, ProductTypeName = GiftSetting.ProductTypeName, Remark = GiftSetting.Remark, UpdatedUserId = dto.OrgBigSettingEntity.UpdatedUserId, UpdatedUserName = dto.OrgBigSettingEntity.UpdatedUserName, UpdatedTime = dto.OrgBigSettingEntity.UpdatedTime })
                    .Where(it => it.Id == dto.OrgBigSettingEntity.Id && it.ProductTypeId== GiftSetting.ProductTypeId).ExecuteCommandAsync() > 0;
            }
            return result;
        }

        /// <summary>
        /// 修改微信AppId及秘钥
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateWxAppSetting(OrgBigSettingEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgBigSettingEntity>().SetColumns(it => new OrgBigSettingEntity() { WxMPAppId = entity.WxMPAppId.Trim(), WxMPAppSecret = entity.WxMPAppSecret.Trim(), UpdatedTime = DateTime.Now, UpdatedUserId = entity.UpdatedUserId, UpdatedUserName = entity.UpdatedUserName })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }

        /// <summary>
        /// 修改微信小程序主题设置
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<bool> UpdateThemeSetting(OrgBigSettingEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgBigSettingEntity>()
                .SetColumns(it => new OrgBigSettingEntity() { WxMPTheme = entity.WxMPTheme,UpdatedTime=DateTime.Now,UpdatedUserId=entity.UpdatedUserId,UpdatedUserName=entity.UpdatedUserName })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }
        /// <summary>
        /// 修改电子合同审核设置
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> UpdateEConSetting(OrgBigSettingEntity entity)
        {
            var result = await _unitOfWork.ModifyClient.Updateable<OrgBigSettingEntity>()
                .SetColumns(it => new OrgBigSettingEntity() { IsVerify= entity.IsVerify, UpdatedTime = DateTime.Now, UpdatedUserId = entity.UpdatedUserId, UpdatedUserName = entity.UpdatedUserName })
                .Where(it => it.Id == entity.Id).ExecuteCommandAsync() > 0;
            return result;
        }
    }
}
